Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Drawing
Imports System.Data
Imports System.Text
Imports System.Windows.Forms

Namespace DgvFilterPopup
	Public Partial Class DgvMonthYearColumnFilter
		Inherits DgvBaseColumnFilter


		Private Shared mMonthCsvList As String = "January,February,March,April,May,June,July,August,September,October,November,December"
		Private mYearMin As Integer
		Private mYearMax As Integer


		#Region "PROPERTIES"

		''' <summary>
		''' Gets or sets the month comma separated list. 
		''' </summary>
		''' <value>The month CSV list.</value>
		''' <remarks>
		''' Allows you to set once your culture-specific comma separated list of months. 
		''' </remarks>
		Public Shared Property MonthCsvList() As String
			Get
				Return DgvMonthYearColumnFilter.mMonthCsvList
			End Get
			Set
				DgvMonthYearColumnFilter.mMonthCsvList = value
			End Set
		End Property

		''' <summary>
		''' Gets or sets the minimum year shown in the years combo.
		''' </summary>
		''' <value>The year min.</value>
		Public Property YearMin() As Integer
			Get
				Return mYearMin
			End Get
			Set
				mYearMin = value
				SetYearList()
			End Set
		End Property

		''' <summary>
		''' Gets or sets the maximum year shown in the years combo.
		''' </summary>
		''' <value>The year min.</value>
		Public Property YearMax() As Integer
			Get
				Return mYearMax
			End Get
			Set
				mYearMax = value
				SetYearList()
			End Set
		End Property

		#End Region


		''' <summary>
		''' Initializes a new instance of the <see cref="DgvMonthYearColumnFilter"/> class.
		''' </summary>
		Public Sub New()
			InitializeComponent()
			mYearMin = DateTime.Today.Year - 10
			mYearMax = DateTime.Today.Year
			'Month combo initializing
            m_comboBoxMonth.Items.Add("---")
            m_comboBoxMonth.Items.AddRange(mMonthCsvList.Split(","c))
            m_comboBoxMonth.SelectedIndex = 0

            'Year combo initializing
            mYearMin = DateTime.Today.Year - 10
            mYearMax = DateTime.Today.Year

            SetYearList()
        End Sub

        ''' <summary>
        ''' Gets the ComboBox control containing the months list.
        ''' </summary>
        Public ReadOnly Property ComboBoxMonth() As ComboBox
            Get
                Return m_comboBoxMonth
            End Get
        End Property


        ''' <summary>
        ''' Gets the ComboBox control containing the years list.
        ''' </summary>
        Public ReadOnly Property ComboBoxYear() As ComboBox
            Get
                Return m_comboBoxYear
            End Get
        End Property

        ''' <summary>
        ''' Perform filter initialitazion and raises the FilterInitializing event.
        ''' </summary>
        ''' <param name="sender">The source of the event.</param>
        ''' <param name="e">The <see cref="System.ComponentModel.CancelEventArgs"/> instance containing the event data.</param>
        ''' <remarks>
        ''' When this <i>column filter</i> control is added to the <i>column filters</i> array of the <i>filter manager</i>,
        ''' the latter calls the <see cref="DgvBaseColumnFilter.Init"/> method which, in turn, calls this method.
        ''' You can ovverride this method to provide initialization code or you can create an event handler and 
        ''' set the <i>Cancel</i> property of event argument to true, to skip standard initialization.
        ''' </remarks>
        Protected Overrides Sub OnFilterInitializing(sender As Object, e As CancelEventArgs)
            MyBase.OnFilterInitializing(sender, e)
            If e.Cancel Then
                Return
            End If
            AddHandler m_comboBoxMonth.SelectedValueChanged, New EventHandler(AddressOf onFilterChanged)
            AddHandler m_comboBoxYear.SelectedValueChanged, New EventHandler(AddressOf onFilterChanged)
            m_comboBoxYear.SelectedIndex = m_comboBoxYear.Items.Count - 1
            Me.FilterHost.RegisterComboBox(m_comboBoxMonth)
            Me.FilterHost.RegisterComboBox(m_comboBoxYear)
        End Sub

        ''' <summary>
        ''' Builds the filter expression and raises the FilterExpressionBuilding event
        ''' </summary>
        ''' <param name="sender">The event source.</param>
        ''' <param name="e">The <see cref="System.ComponentModel.CancelEventArgs"/> instance containing the event data.</param>
        ''' <remarks>
        ''' Override <b>OnFilterExpressionBuilding</b> to provide a filter expression construction
        ''' logic and to set the values of the <see cref="DgvBaseColumnFilter.FilterExpression"/> and <see cref="DgvBaseColumnFilter.FilterCaption"/> properties.
        ''' The <see cref="DgvFilterManager"/> will use these properties in constructing the whole filter expression and to change the header text of the filtered column.
        ''' Otherwise, you can create an event handler and set the <i>Cancel</i> property of event argument to true, to skip standard filter expression building logic.
        ''' </remarks>
        Protected Overrides Sub OnFilterExpressionBuilding(sender As Object, e As CancelEventArgs)
            MyBase.OnFilterExpressionBuilding(sender, e)
            If e.Cancel Then
                FilterManager.RebuildFilter()
                Return
            End If

            Dim MinDate As DateTime, MaxDate As DateTime

            If m_comboBoxMonth.SelectedIndex = 0 Then
                MinDate = New DateTime(mYearMin + m_comboBoxYear.SelectedIndex, 1, 1, 0, 0, 0)
                MaxDate = MinDate.AddYears(1)
                FilterCaption = (OriginalDataGridViewColumnHeaderText & vbLf & " = ") + m_comboBoxYear.Text
            Else
                MinDate = New DateTime(mYearMin + m_comboBoxYear.SelectedIndex, m_comboBoxMonth.SelectedIndex, 1, 0, 0, 0)
                MaxDate = MinDate.AddMonths(1)
                FilterCaption = ((OriginalDataGridViewColumnHeaderText & vbLf & " = ") + m_comboBoxMonth.Text & " ") + m_comboBoxYear.Text
            End If

            FilterExpression = Convert.ToString(Me.DataGridViewColumn.DataPropertyName) & " >= " & FormatValue(MinDate, Me.ColumnDataType) & " AND " & Convert.ToString(Me.DataGridViewColumn.DataPropertyName) & " < " & FormatValue(MaxDate, Me.ColumnDataType)
            FilterManager.RebuildFilter()
            Return
        End Sub


        Private Sub onFilterChanged(sender As Object, e As EventArgs)
            If Not FilterApplySoon OrElse Not Me.Visible Then
                Return
            End If
            Active = True
            FilterExpressionBuild()
        End Sub


        Private Sub SetYearList()
            m_comboBoxYear.Items.Clear()
            For y As Integer = mYearMin To mYearMax
                m_comboBoxYear.Items.Add(y.ToString())
            Next
            m_comboBoxYear.SelectedIndex = m_comboBoxYear.Items.Count - 1
        End Sub
	End Class
End Namespace
